<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <avue-crud 
        ref="crud" 
        v-model="form" 
        :page="page" 
        :data="tableData" 
        :table-loading="tableLoading"
        :option="tableOption" 
        :before-open="handleOpenBefore" 
        @on-load="getList" 
        @search-change="searchChange"
        @search-reset="resetsearch" 
        @refresh-change="refreshChange" 
        @size-change="sizeChange"
        @current-change="currentChange" 
        @row-update="handleUpdate" 
        @row-save="handleSave" 
        @row-del="rowDel">
        <template #way-form>
          <el-select v-model="form.way" placeholder="请选择报警方式">
            <el-option v-for="(item, index) in alarm_way" :key="index" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </template>
      </avue-crud>
    </div>
  </div>
</template>

<script lang="ts" name="alarmSetting" setup>
import {
  fetchList, putObj, addObj
} from '/@/api/basiciot/area/buildings';
import { tableOption } from '/@/crud/alarm/alarmsetting';


import { validateNull } from '/@/utils/validate';
import { useMessage, useMessageBox } from '/@/hooks/message';
import { useDict } from '/@/hooks/dict';

const { alarm_way } = useDict('alarm_way');

console.log(alarm_way)
const form:any = ref({})
const tableLoading = ref(false)

const page:any = ref({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10, // 每页显示多少条
})
let tableData = ref([])


onMounted(() => {
})

//初始化获取数据
const getList = (page: any, params?: object) => {
  // tableLoading.value = true;
  fetchList(
    Object.assign(
      {
        current: page.currentPage,
        size: page.pageSize,
      },
      params
    )
  )
    .then((res) => {

    })
    .catch(() => {
      tableLoading.value = false;
    });
}

//打开弹框
const handleOpenBefore = (show: Function, type: string) => {

  show();
}

const rowDel = (row:any, index:number) => {


  useMessageBox().confirm('是否确认删除' + row.id).then((res) => {


    useMessage().success('删除成功');
  }).catch(() => {

  })
}
//更新操作
const handleUpdate = (row:any, index:number, done:Function, loading:Function) => {
  putObj(row).then((data) => {
  if (data.data) {
      useMessage().success('修改成功');
    } else {
      useMessage().error('修改失败');
    }
    done();
    getList(page);
  })
  .catch(() => {
    loading();
  });
}
//新增操作
const handleSave = (row:any, done:Function, loading:Function) => {
  
  addObj(row)
    .then((res) => {
      if (res.data) {
        useMessage().success('添加成功');
      } else {
        useMessage().success('添加失败');
      }

      done();
      getList(page);
    })
    .catch(() => {
      loading();
    });
}
const sizeChange = (pageSize:number) => {
  page.pageSize = pageSize;
}
const currentChange = (current:number) => {
  page.currentPage = current;
}
const searchChange = (form:object, done:Function) => {
  page.currentPage = 1;
  getList(page, form);
  done();
}
const refreshChange = () => {
  getList(page);
}
const resetsearch = () =>{
}
</script>
<style lang="scss" scoped>


</style>
